//
//  MetaNameController.m
//  iGSS
//
//  Copyright 2010 Electronic Business Systems Ltd.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#import "MetaNameController.h"
#import "FormattingUtilities.h"
#import "GSSCommunicator.h"
#import "GSSObject.h"

@implementation MetaNameController

- (NSInteger)numberOfRows {
    return 1;
}

- (UITableViewCell *)cellForRow:(NSInteger)row {
    static NSString *CellIdentifier = @"MetaNameCell";
    
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                       reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:16.0];
        cell.textLabel.numberOfLines = 2;
        cell.textLabel.lineBreakMode = UILineBreakModeMiddleTruncation;
        cell.backgroundColor = [UIColor clearColor];
        UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
        backgroundView.backgroundColor = [UIColor clearColor];
        cell.backgroundView = backgroundView;
        [backgroundView release];
        
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    cell.imageView.image = object.icon;
    cell.textLabel.text = object.name;
    
    if (object.type == GSSObjectTypeFile) {
        cell.detailTextLabel.text = [FormattingUtilities byteStringForSize:object.size];
    } else if (object.size && [object.children count]) {
        cell.detailTextLabel.text = [NSString stringWithFormat:
                                     LocalizedString(@"{Bytes used}"),
                                     [FormattingUtilities byteStringForSize:object.size],
                                     [object.children count]];        
    } else {
        cell.detailTextLabel.text = nil;
    }
    
    if (self.isEditing)
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    else
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (CGFloat)heightForRow:(NSInteger)row {
    return 60;
}

- (void)didSelectRow:(NSInteger)row {
    if (!self.isEditing)
        return;

	EditNameController *editNameController = [[EditNameController alloc] initWithStyle:UITableViewStyleGrouped];
	editNameController.name = object.name;
	editNameController.delegate = self;
	[self.navigationController pushViewController:editNameController animated:YES];
	[editNameController release];    
}


//MARK: -
//MARK: EditNameDelegate

- (void)nameEdited:(NSString *)name {
    [[GSSCommunicator defaultCommunicator] setName:name ofObject:object];
    object.name = name;
    [object save];
}

@end
